/********************************************************************************************************************************************************************************
* 
* Class Name  	: 
* Version 	  	: 
* Description 	: 
* 
********************************************************************************************************************************************************************************
* 
* Author 		: Karim Beyrouti
* Date 			: 
* 
********************************************************************************************************************************************************************************
* 
* METHODS
* 
*
* PROPERTIES
* 
*
* EVENTS
* 
* 
********************************************************************************************************************************************************************************
* 				:
*
*
*********************************************************************************************************************************************************************************
* NOTES			:
**********************************************************************************************************************************************************************************/

package com.kurst.utils {

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import mx.core.UIComponent;

	public class DoubleClickManager implements IEventDispatcher {


		private var _time_out				: int 				= 300;
		private var component				: UIComponent 		= null;
		private var eventDispatcher			: IEventDispatcher 	= null;
		private var timer					: Timer 			= null;
		private var storedEvent				: Event 			= null;
		
		public function DoubleClickManager(component:UIComponent) {
			
			super();
			
			timer 					= new Timer( _time_out , 1 );
			timer.addEventListener( TimerEvent.TIMER_COMPLETE , deferredSingleClick , false , 0 , true );
			
			addComponent( component )

			//component.addEventListener( MouseEvent.CLICK , singleClick , false , 0 , true );
			//component.addEventListener( MouseEvent.DOUBLE_CLICK , doubleClick , false , 0 , true );
			
		}

		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		//-PUBLIC-----------------------------------------------------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------------------------------------------------------------

		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function addComponent ( component : UIComponent ) : void {
			
			if ( component != null ){
				
				component.removeEventListener(MouseEvent.CLICK, singleClick);
				component.removeEventListener(MouseEvent.DOUBLE_CLICK, doubleClick);
				
			}
			
			eventDispatcher			= new EventDispatcher(this.component);
			
			component						= component;
			component.doubleClickEnabled	= true;
			
			component.addEventListener( MouseEvent.CLICK , singleClick , false , 0 , true );
			component.addEventListener( MouseEvent.DOUBLE_CLICK , doubleClick , false , 0 , true );
			
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function destroy () : void {
			
			timer.stop();
			timer.reset();
		
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, deferredSingleClick );
			component.removeEventListener(MouseEvent.CLICK, singleClick);
			component.removeEventListener(MouseEvent.DOUBLE_CLICK, doubleClick);
			
			component 			= null;
			storedEvent 		= null;
			eventDispatcher 	= null;
			
		}
			
		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		//-PRIVATE----------------------------------------------------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------------------------------------------------------------

		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		private function stopTimer():Event {
			
			var lc_storedEvent:Event = storedEvent;
				storedEvent = null;
			
			if (timer != null) {
				
				timer.stop();
				timer.reset();
				
			}
			
			return lc_storedEvent;
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		private function startTimer(event:Event):void {
			
			timer.stop();
			timer.reset();
			storedEvent = event;
			
	        timer.start();
		}
		


		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		//-GET/SET----------------------------------------------------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function get doubleClickDelay() : int {
			
			return _time_out;
			
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function set doubleClickDelay(time_out : int) : void {
			
			_time_out = time_out;
			
		}
		
		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		//-EVENT HANDLERS-------------------------------------------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------------------------------------------------------------

		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		private function singleClick(event:MouseEvent):void {
			
			startTimer(event);
			
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
	    private function deferredSingleClick(event:TimerEvent = null):void {
	    	var lc_event:Event = stopTimer();
			dispatchEvent(lc_event);
	    }
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		private function doubleClick(event:MouseEvent):void {
			stopTimer();
			dispatchEvent(event);
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
			eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function dispatchEvent(event:Event):Boolean {
			return eventDispatcher.dispatchEvent(event);
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function hasEventListener(type:String):Boolean {
			return eventDispatcher.hasEventListener(type);
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
			eventDispatcher.removeEventListener(type, listener, useCapture);
		}
		/**
		* @method 
		* @tooltip
		* @param
		* @return
		*/
		public function willTrigger(type:String):Boolean {
			return eventDispatcher.willTrigger(type);
		}

	}
}
